<?php


namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use App\Models\Mining\DigRatio;
use Illuminate\Http\Request;
use App\Models\ComputeSet;

class ComputeController extends Controller
{

    public function showCount(Request $request){

        $data['work_kj']=ComputeSet::where("type",1)->select('phs','tech_fee','work_num','period_day')->first()->toArray();
        if(!empty($data['work_kj']['phs'])){
            $data['work_kj']['phs']=explode("/",$data['work_kj']['phs']);
        }else{
            $data['work_kj']['phs']=[];
        }
        $data['work_kj']['period_day']= bcdiv($data['work_kj']['period_day'],365);
        $data['work_yun']=ComputeSet::where("type",2)->select('phs_yun','tech_fee')->first()->toArray();
        $data['work_yun']['phs']=[];

        if($data){
            return $this->ajaxSuccess($data);
        }else{
            return  $this->ajaxSuccess([]);
        }

    }


    public function doCount(Request $request){

        $data = $this->decryptData($request->post());
        if($data['phs'] <= 0) return $this->ajaxError('算力值須大於0');
        $find=ComputeSet::where("type",$data['type'])->first();
        $ratio=0.5;
        $data['tech_fee'] = bcdiv($data['tech_fee'],100,2);
        if(!empty($find['phs_ratio']))$ratio=$find['phs_ratio'];
        if($data['type'] == 1){
            $data['custody_fee'] = bcmul($data['custody_fee'],$data['work_num'],8);
            $return['money']=bcmul($data['work_num'],$data['work_price'],2);
            $phs=bcmul($data['phs'],$data['work_num'],2);
            $return['has_phs']=bcmul($phs,$ratio,2);
            $dayGet=bcmul($return['has_phs']*$data['work_get'],1-$data['tech_fee'],8);
            $yearPay=$return['money'];
            $return['year_get']=bcdiv($dayGet*365*$data['fil_price'],$data['custody_fee']+$yearPay,2);
            $return['total_get']=bcsub($dayGet*$find->period_day*$data['fil_price'],$data['custody_fee']*6,2);
        }else{
            $return['money']=bcmul($data['phs'],$data['work_price'],2);
            $return['has_phs']=bcmul($data['phs'],$ratio,2);
            $dayGet=bcmul($return['has_phs']*$data['work_get'],1-$data['tech_fee'],8);
            $return['year_get']=bcdiv($dayGet*365*$data['fil_price'],$return['money'],2);
            $return['total_get']=bcmul($dayGet*$find->period_day,$data['fil_price'],2);
        }
        $return['year_get']=bcmul($return['year_get'],100,2);
        $moneyOne=bcmul($dayGet,$data['fil_price'],8);
        $return['refund_time']=bcdiv($return['money'],$moneyOne,2);
        $return['month_get']=bcmul($dayGet,30,2);
        $monthGet=bcmul($dayGet,30,8);
        $fanOne=bcmul(0.25,$dayGet,8);
        $fangTwo=bcdiv(0.75*$dayGet,180,8);
        $return['month_fang']=bcadd(bcmul($fanOne,180,8),bcmul($fangTwo,435,8),2);
        $return['three_fang']=bcadd(bcmul($fanOne,580,8),bcmul($fangTwo,4005,8),2);
        $return['six_fang']=bcadd(bcmul($fanOne,1080,8),bcmul($fangTwo,16110,8),2);
        $six_fang=bcadd(bcmul($fanOne,180,8),bcmul($fangTwo,16110,8),8);
        $year_fang=bcmul($dayGet,184,8);
        $year_fang=bcadd($six_fang,$year_fang,8);
        $return['year_fang']=bcmul($dayGet,540,8);
        $return['year_suo']=bcmul($dayGet,1080,8);
        if($return){
            return  $this->ajaxSuccess($return);
        }else{
            return $this->ajaxSuccess([]);
        }
    }

}
